﻿using System;
using System.Collections.Generic;
using System.Reflection;


namespace lib472
{
    public class Tools
    {

        /// <summary>
        ///  // 使用反射获取对象的所有属性
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static Dictionary<string, string> GetFormDataList(object obj)
        {
            
            var formDataList =  new Dictionary<string, string>();

                if (obj != null)
                {
                    // 使用反射获取对象的所有属性
                    PropertyInfo[] properties = obj.GetType().GetProperties();

                    // 遍历每个属性并添加到 formDataList
                    foreach (var property in properties)
                    {
                        object value = property.GetValue(obj);

                        // 如果值不为 null，将属性名和属性值添加到 formDataList
                        if (value != null)
                        {
                            formDataList.Add(property.Name, value.ToString());
                        }
                    }
                }
            return formDataList;
        }


        public static bool IsVerifyDateTime(string startDateTime, string endDateTime,ref string msg)
        {

            DateTime startDate, lastDate;

            if (!DateTime.TryParse(startDateTime, out startDate))
            {
                msg = "時間格式錯誤！";
                return false;
            }
            if (!DateTime.TryParse(endDateTime, out lastDate))
            {
                msg = "時間格式錯誤！";
                return false;
            }

            if (lastDate < startDate)
            {
                msg = "日期輸入錯誤，起始日期大於截止日期！";
                return false;
            }
            return true;
        }


        public static string GetBarcode3String(string barcode1, string barcode2, string payType, string payMoney)
        {
            string barcode = barcode1 + barcode2 + payType + "R" + payMoney;
            int sum = 0;

            foreach (char ch in barcode)
            {
                if (ch >= '0' && ch <= '9') sum += ch - 48;
                if (ch >= 'A' && ch <= 'Z') sum += ch - 55;
            }

            int checkNum = sum % 36;
            char checkCh = '\0';

            if (checkNum < 10)
            {
                checkCh = (char)(checkNum + 48);
            }
            else
            {
                checkCh = (char)(checkNum + 55);
            }

            return payType + "R" + checkCh.ToString() + payMoney;
        }
    }
}
